package cn.gemframe.config.security.bean;

import cn.gemframe.constant.GemConstant;
import lombok.Data;
import org.codehaus.jackson.annotate.JsonIgnore;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class GemUserDetails implements UserDetails {

	private static final long serialVersionUID = 6071250038624543678L;

	private String appID;
    private String appSecret;

	private String userId;
	private String username;
	private String password;
	private boolean enabled = true;
	private boolean accountNonExpired = true;
	private boolean accountNonLocked = true;
	private boolean credentialsNonExpired = true;
	@JsonIgnore
	private List<String> permits;
	private Collection<? extends GrantedAuthority> authorities=AuthorityUtils.commaSeparatedStringToAuthorityList("");
	/***************************扩容字段********************************/
	private Map<String,Object> hashMap=new HashMap<String,Object>();
	
	public GemUserDetails() {}
	public GemUserDetails(String username, String password) {
		this.username=username;
		this.password=password;
	}


	public static GemUserDetails getUserData() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		if (authentication == null) {
			return null;
		}
		Object principal = authentication.getPrincipal();
		if (principal == null || GemConstant.Auth.ANONY_MOUS_USER.equalsIgnoreCase(principal.toString())) {
			return null;
		}
		GemUserDetails userData=(GemUserDetails) principal;
		return userData;
	}
}
